package org.blogwu.sso.until.redisUntil;



import java.util.List;
import java.util.Map;

import org.blogwu.sso.until.string.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * <p>此类暂时已弃用</p>
 * @ClassName: RedisUtil2
 * @Description: redis封装工具类   集成spring   适用于集群使用
 * @author Administrator
 * @date 2016年10月23日
 *
 */
//@Component
public class RedisUtil2 {

	//@Autowired
	private  ShardedJedisPool jedisPool;
	
	public ShardedJedisPool getJedisPool() {
		return jedisPool;
	}


	public void setJedisPool(ShardedJedisPool jedisPool) {
		//System.out.println("2现在存在最大链接额数： "+jedisPool.getNumActive());
	//	if (this.jedisPool == null) {
			this.jedisPool = jedisPool;
	//	}
	}
	
	public ShardedJedis getJedis() {
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = getJedisPool().getResource();
		} catch (Exception e) {
			throw new JedisConnectionException(e);
		}
		return shardedJedis;
	}

	public void returnResource(ShardedJedis shardedJedis, boolean isBroken) {
		if (shardedJedis == null)
			return;
		if (isBroken)
			getJedisPool().returnBrokenResource(shardedJedis);
		else
			getJedisPool().returnResource(shardedJedis);
	}

	

	/**
	 * <p>通过key获取储存在redis中的value</p>
	 * <p>并释放连接</p>
	 * @param key
	 * @return 成功返回value 失败返回null
	 */
	public String get(String key){
		ShardedJedis shardedJedis = null;
		String value = null;
		try {
			shardedJedis = jedisPool.getResource();
			value = shardedJedis.get(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return value;
	}
	
	
	
	
	/**
	 * <p>向redis存入key和value,并释放连接资源</p>
	 * <p>如果key已经存在会返回1</p>
	 * @param key
	 * @param value
	 * @return 成功 返回OK 失败返回 0   如果key存在则返回1
	 */
	public String set(String key,String value){
		ShardedJedis shardedJedis = null;
		try {
			System.out.println(jedisPool.getNumActive());
			shardedJedis = jedisPool.getResource();
			String valueTemp=shardedJedis.get(key);
			if(!StringUtils.isOk(valueTemp)){
				return "1";
			}
			return shardedJedis.set(key, value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
			return "0";
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
	}
	

	/**
	 * <p>向redis存入key和value,并释放连接资源</p>
	 * <p>如果key已经存在 则覆盖</p>
	 * @param key
	 * @param value
	 * @return 成功 返回OK 失败返回 0
	 */
	public String setWithSel(String key,String value){
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = jedisPool.getResource();
			return shardedJedis.set(key, value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
			return "0";
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
	}
	
	/**
	 * <p>向redis存入key和value,设置失效时间,并释放连接资源</p>
	 * <p>如果key已经存在 则覆盖</p>
	 * @param key
	 * @param value
	 * @param time   失效时间   单位秒s
	 * @return 成功 返回OK 失败返回 0
	 */
	public String set(String key,String value,int time){
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = jedisPool.getResource();
			return shardedJedis.setex(key,time,value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
			return "0";
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
	}
	

	
	
	/**
	 * <p>设置key的值,并返回一个旧值</p>
	 * @param key
	 * @param value
	 * @return 旧值 如果key不存在 则返回null
	 */
	public String getset(String key,String value){
		ShardedJedis shardedJedis = null;
		String res = null;
		try {
			shardedJedis = jedisPool.getResource();
			res = shardedJedis.getSet(key, value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return res;
	}
	
/**map **/
	
	/**
	 * <p>通过key 和 fields 获取指定的value 如果没有对应的value则返回null</p>
	 * @param key
	 * @param fields 可以使 一个String 也可以是 String数组
	 * @return 
	 */
	public List<String> hmget(String key,String...fields){
		ShardedJedis shardedJedis = null;
		List<String> res = null;
		try {
			shardedJedis = jedisPool.getResource();
			res = shardedJedis.hmget(key, fields);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return res;
	}
	
	/**
	 * <p>通过key获取所有的field和value  如果无 则为null</p>
	 * @param key
	 * @return
	 */
	public Map<String, String> hgetall(String key){
		ShardedJedis shardedJedis = null;
		Map<String, String> res = null;
		try {
			shardedJedis = jedisPool.getResource();
			res = shardedJedis.hgetAll(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return res;
	}
	
	
	/**
	 * <p>通过key同时设置 hash的多个field(map集合)</p>
	 * @param key
	 * @param hash   map
	 * @return 返回OK 异常返回null
	 */
	public String hmset(String key,Map<String, String> hash){
		ShardedJedis shardedJedis = null;
		String res = null;
		try {
			shardedJedis = jedisPool.getResource();
			res = shardedJedis.hmset(key, hash);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return res;
	}
	
	/**map end**/	
	
	/**list **/
	/**
	 * <p>通过key返回所有和key有关的value</p>
	 * @param key
	 * @return
	 */
	public List<String> hvals(String key){
		ShardedJedis shardedJedis = null;
		List<String> res = null;
		try {
			shardedJedis = jedisPool.getResource();
			res = shardedJedis.hvals(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return res;
	}

	/**
	 * <p>通过key向list头部添加字符串</p>
	 * @param key
	 * @param strs 可以使一个string 也可以使string数组
	 * @return 返回list的value个数
	 */
	public Long lpush(String key ,String...strs){
		ShardedJedis shardedJedis = null;
		Long res = null;
		try {
			shardedJedis = jedisPool.getResource();
			res = shardedJedis.lpush(key, strs);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return res;
	}
	
	/**
	 * <p>通过key向list尾部添加字符串</p>
	 * @param key
	 * @param strs 可以使一个string 也可以使string数组
	 * @return 返回list的value个数
	 */
	public Long rpush(String key ,String...strs){
		ShardedJedis shardedJedis = null;
		Long res = null;
		try {
			shardedJedis = jedisPool.getResource();
			res = shardedJedis.rpush(key, strs);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(shardedJedis);
			e.printStackTrace();
		} finally {
			returnResource(jedisPool, shardedJedis);
		}
		return res;
	}
	/**list end **/
	
	/**
	 * 返还到连接池
	 * 
	 * @param pool
	 * @param redis
	 */
	public static void returnResource(ShardedJedisPool jedisPool, ShardedJedis shardedJedis) {
		if (shardedJedis != null) {
			jedisPool.returnResource(shardedJedis); 
		}
	}
	

	/**
	 * 返还到连接池
	 * @param redis
	 */
	public static void returnResource2(ShardedJedisPool jedisPool, ShardedJedis shardedJedis) {
		if (shardedJedis != null) {
			jedisPool.returnResource(shardedJedis); 
		}
	}
	
	
	
}
